package cool.taomu.toolkit.kademlia.utils

import cool.taomu.toolkit.utils.StringUtils
import java.math.BigInteger
import java.security.SecureRandom

class KademliaUtils {
	//val static LOG = LoggerFactory.getLogger(KademliaUtils);

	def static createNodeId() {
		var random = new SecureRandom();
		var bytes = newByteArrayOfSize(20);
		random.nextBytes(bytes);
		val binaryStringBuilder = new StringBuilder();
		bytes.forEach [
			var uint = it.bitwiseAnd(0xFF);
			var str = String.format("%8s", Integer.toBinaryString(uint)).replace(' ', '0');
			binaryStringBuilder.append(str);
		]
		return binaryStringBuilder.toString();
	}

	
	def static stringTo160Bit(String string) {
		return StringUtils.stringTo160Bit(string)
	}
	
		// 计算两个节点之间的XOR距离
	def static BigInteger xorDistance(String id1, String id2) {
		var BigInteger bigInt1 = new BigInteger(id1, 2);
		var BigInteger bigInt2 = new BigInteger(id2, 2);
		return bigInt1.xor(bigInt2);
	}

	

}
